package edu.suda.xsap.service.impl;

import edu.suda.xsap.dto.MemberCardDto;
import edu.suda.xsap.dto.convert.MemberCardConvert;
import edu.suda.xsap.entity.MemberBindRecord;
import edu.suda.xsap.entity.MemberCard;
import edu.suda.xsap.entity.MemberLog;
import edu.suda.xsap.entity.vo.BIndVo;
import edu.suda.xsap.entity.vo.CardNewVo;
import edu.suda.xsap.entity.vo.CardVo;
import edu.suda.xsap.entity.vo.NewCardVo;
import edu.suda.xsap.mapper.MemberCardMapper;

import edu.suda.xsap.mapper.MemberLogMapper;
import edu.suda.xsap.service.MemberCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import edu.suda.xsap.mapper.MemberBindRecordMapper;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MemberCardServiceImpl implements MemberCardService {
    @Autowired
    private MemberCardMapper memberCardMapper;
    @Autowired
    private MemberCardConvert memberCardConvert;
    @Autowired
    private MemberBindRecordMapper memberBindRecordMapper;
    @Autowired
    private MemberLogMapper memberLogMapper;

    @Override
    public void delete(Integer id) {
        memberCardMapper.delete(id);
    }

    @Override
    public List<MemberCardDto> findAll() {
        return memberCardConvert.toDtoList(memberCardMapper.findAll());
    }

    @Override
    public MemberCardDto findById(Integer id) {
        return memberCardConvert.toDto(memberCardMapper.findById(id));
    }



    @Override
    public void save(MemberCard memberCard) {
        memberCardMapper.save(memberCard);
    }

    @Override
    public void update(MemberCard memberCard) {
        memberCardMapper.update(memberCard);
    }

    @Override
    public void bind(BIndVo vo) {
        MemberBindRecord memberBindRecord = MemberBindRecord.builder()
                .cardId(vo.getCardId())
                .memberId(vo.getMemberId())
                .receivedMoney(vo.getReceivedMoney())
                .validCount(vo.getValidCount())
                .validDay(vo.getValidDay())
                .note(vo.getNote())
                .payMode(vo.getPayMode())
                .createTime(LocalDateTime.now())
                .lastModifyTime(LocalDateTime.now())
                .version(1)
                .build();
        memberBindRecordMapper.bind(memberBindRecord);
        Integer MemberBindID=memberBindRecord.getId();
        MemberLog memberLog = MemberLog.builder()
                .type("绑定")
                .memberBindId(MemberBindID)
                .createTime(LocalDateTime.now())
                .lastModifyTime(LocalDateTime.now())
                .note(memberBindRecord.getNote())
                .version(1)
                .involvedMoney(memberBindRecord.getReceivedMoney())
                .cardCountChange(memberBindRecord.getValidCount())
                .cardDayChange(memberBindRecord.getValidDay())
                .build();
        memberLogMapper.save(memberLog);

    }

    @Override
    public Integer findCount() {
        return memberCardMapper.findCount();
    }

    @Override
    public List<CardVo> findByLimit(Integer limit, Integer offset) {
        return memberCardMapper.findByLimit(limit,offset);
    }

    @Override
    public void save1(NewCardVo vo) {
        memberCardMapper.save1(vo);
        Integer id=memberCardMapper.getLastId();
        System.out.println(id);
        for(Integer courseId:vo.getCourses()){
            memberCardMapper.bind(id,courseId);
        }
    }

    @Override
    public CardNewVo findId(Integer Id) {
        return memberCardMapper.findId(Id);
    }
}
